<?php
include_once("../../untils/conn.php");
mysqli_query($con, "set names utf8");

$type = $_GET['type'];

// 将POST的数据写入log.txt文件
$logFile = __DIR__ . "/log.txt";
$logContent = date('Y-m-d H:i:s') . " [$type] " . json_encode($_POST) . PHP_EOL;
file_put_contents($logFile, $logContent, FILE_APPEND);

if ($type === 'haokeyun') {
    // 检查是否为测试消息
    $msg = $_POST['msg'];
    if ($msg === 'check') {
        echo 'SUCCESS';
        exit; 
    }
    
    $oderid = $_POST['order_sn']; //订单号
    $status = $_POST['status'];  //订单状态
    $thirdphone = $_POST['number']; //生产号码
    $is_activated = $_POST['is_activated']; //激活状态
    $activated_at = $_POST['activated_at'];  //激活时间
    $is_recharged = $_POST['is_recharged'];  //首冲状态
    $recharged_at = $_POST['recharged_at']; //首冲时间
    $recharged_amount = $_POST['recharged_amount']; //首冲金额
    $express_name = $_POST['express_name'];  //物流名称
    $express_sn = $_POST['express_sn'];  //物流单号
    $reason = $_POST['reason'];  //失败原因
    
    if ($status === '26' && $is_activated === '1') {
        // 更新数据oder表
        $query = "UPDATE oder SET status = '3', card_sta = '1' WHERE oderid = '$oderid'";
        $querys = "UPDATE revenue SET oder_sta = '3' WHERE oderid = '$oderid'";
        $result = mysqli_query($con, $query);
        $result2 = mysqli_query($con, $querys);
        if ($result && $result2) {
            echo 'success';
        } else {
            echo 'err';
        }
    } elseif ($status === '30' || $status === '21') {
        // 更新数据oder表
        $query = "UPDATE oder SET status = '4', beizhu = '$reason' WHERE oderid = '$oderid'";
        $querys = "UPDATE revenue SET oder_sta = '4' WHERE oderid = '$oderid'";
        $result = mysqli_query($con, $query);
        $result2 = mysqli_query($con, $querys);
        if ($result && $result2) {
            echo 'success';
        } else {
            echo 'err';
        }
    } elseif ($status === '26') {
        // 更新数据oder表
        $beizhu = $express_name . ' ' . $express_sn;
        $query = "UPDATE oder SET status = '2', beizhu = '$beizhu', thirdphone = '$thirdphone' WHERE oderid = '$oderid'";
        $querys = "UPDATE revenue SET oder_sta = '2' WHERE oderid = '$oderid'";
        $result = mysqli_query($con, $query);
        $result2 = mysqli_query($con, $querys);
        if ($result && $result2) {
            echo 'success';
        } else {
            echo 'err';
        }
    } else {
        echo 'SUCCESS'; // 其他情况默认返回成功
    }

} elseif ($type === 'gantanhao') {
    // 检查是否为测试消息
  $msg = $_POST['msg'];
    if ($msg === 'success') {
        echo 'SUCCESS';
        exit;
    }
    
    $outer_id = $_POST['outer_id']; // 合作方id
    $plan_mobile_produced = $_POST['plan_mobile_produced']; // 生产号码
    $status = $_POST['status'];  // 订单状态
    $is_activated = $_POST['is_activated']; // 激活状态
    $activated_at = $_POST['activated_at'];  // 激活时间
    $is_recharged = $_POST['is_recharged'];  // 首冲状态
    $recharged_at = $_POST['recharged_at']; // 首冲时间
    $tracking_number = $_POST['tracking_number'];  // 物流单号
    $tracking_company = $_POST['tracking_company'];  // 物流公司名称
    $reason = $_POST['reason'];  // 失败原因
    $sign = $_POST['sign']; // 签名

    // 取出apitoke
    $sql = "SELECT * FROM config_api WHERE groups = 'gantanhao'";
    $result = mysqli_query($con, $sql);

    // 检查查询结果是否为空
    if (mysqli_num_rows($result) > 0) {
        // 获取第一行数据
        $row = mysqli_fetch_assoc($result);
        $userid = $row['userid'];
        $token = $row['token'];
    } else {
        // 处理查询结果为空的情况
    }
    // 验证签名
    $api_token = $token; 
    $signature = md5("outer_id=$outer_id&plan_mobile_produced=$plan_mobile_produced&status=$status$api_token");
    if ($sign !== $signature) {
        echo 'Invalid signature';
        exit;
    }

    if ($is_activated === '1') {
       // 更新数据oder表
        $query = "UPDATE oder SET status = '3', card_sta = '1' WHERE oderid = '$outer_id'";
        $querys = "UPDATE revenue SET oder_sta = '3' WHERE oderid = '$outer_id'";
        $result = mysqli_query($con, $query);
        $result2 = mysqli_query($con, $querys);
        if ($result && $result2) {
            echo 'SUCCESS';
        } else {
            echo 'err';
        }
       
    } elseif ($status === '550') {
        // 更新数据oder表
        $beizhu = $tracking_company . ' ' . $tracking_number;
        $query = "UPDATE oder SET status = '2', beizhu = '$beizhu', thirdphone = '$plan_mobile_produced' WHERE oderid = '$outer_id'";
        $querys = "UPDATE revenue SET oder_sta = '2' WHERE oderid = '$outer_id'";
        $result = mysqli_query($con, $query);
        $result2 = mysqli_query($con, $querys);
        if ($result && $result2) {
            echo 'SUCCESS';
        } else {
            echo 'err';
        }
    } elseif ($status === '-460' || $status === '-120') {
        // 更新数据oder表
        $query = "UPDATE oder SET status = '4', beizhu = '$reason' WHERE oderid = '$outer_id'";
        $querys = "UPDATE revenue SET oder_sta = '4' WHERE oderid = '$outer_id'";
        $result = mysqli_query($con, $query);
        $result2 = mysqli_query($con, $querys);
        if ($result && $result2) {
            echo 'SUCCESS';
        } else {
            echo 'err';
        }
    } else {
        echo 'SUCCESS'; // 其他情况默认返回成功
    }
} else {
    // 未知平台类型，可以进行相应的处理
}

// 返回小写的'SUCCESS'表示成功接收回调请求
// echo 'SUCCESS';
?>

